cmake_minimum_required(VERSION 3.16)
project(asn1c C)

set(CMAKE_C_STANDARD 11)

set(SYSTEM_CPU_COMPILER "${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}_${CMAKE_C_COMPILER_ID}")
string(TOLOWER "${SYSTEM_CPU_COMPILER}" SYSTEM_CPU_COMPILER)
message("SYSTEM_CPU_COMPILER: ${SYSTEM_CPU_COMPILER}")

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

add_definitions(-DASN_DISABLE_OER_SUPPORT=1)

# NGAP
set(DST_DIR ${PROJECT_SOURCE_DIR}/ngap)
set(NGAP_ASN_FILE ${PROJECT_SOURCE_DIR}/asn1Files/38413-g10.asn1)
execute_process(COMMAND ${PROJECT_SOURCE_DIR}/tools/asn1c_compile.sh "${PROJECT_SOURCE_DIR}" "${DST_DIR}" "${NGAP_ASN_FILE}" "NGAP_" "-findirect-choice -fno-include-deps"
        RESULT_VARIABLE ret)
if (NOT ${ret} STREQUAL 0)
    message(FATAL_ERROR "${ret}: error")
endif (NOT ${ret} STREQUAL 0)

include_directories(${PROJECT_SOURCE_DIR}/skeletons)
include_directories(${PROJECT_SOURCE_DIR}/ngap)

file(GLOB_RECURSE NGAP_LIB_SRCS "${PROJECT_SOURCE_DIR}/ngap/*.c")

add_custom_target (
        ngap_flag ALL
        ${PROJECT_SOURCE_DIR}/tools/asn1c_compile.sh "${PROJECT_SOURCE_DIR}" "${DST_DIR}" "${NGAP_ASN_FILE}" "NR_" "-findirect-choice -fno-include-deps"
        DEPENDS  "${NGAP_ASN_FILE}"
)

add_library(ngap_asn1_${SYSTEM_CPU_COMPILER} ${NGAP_LIB_SRCS})
add_dependencies(ngap_asn1_${SYSTEM_CPU_COMPILER} ngap_flag)

# NR RRC
set(DST_DIR ${PROJECT_SOURCE_DIR}/rrc)
set(NGAP_ASN_FILE ${PROJECT_SOURCE_DIR}/asn1Files/nr-rrc-15.6.0.asn1)
execute_process(COMMAND ${PROJECT_SOURCE_DIR}/tools/asn1c_compile.sh "${PROJECT_SOURCE_DIR}" "${DST_DIR}" "${NGAP_ASN_FILE}" "NR_" "-findirect-choice"
        RESULT_VARIABLE ret)
if (NOT ${ret} STREQUAL 0)
    message(FATAL_ERROR "${ret}: error")
endif (NOT ${ret} STREQUAL 0)

include_directories(${PROJECT_SOURCE_DIR}/skeletons)
include_directories(${PROJECT_SOURCE_DIR}/rrc)

file(GLOB_RECURSE RRC_LIB_SRCS "${PROJECT_SOURCE_DIR}/rrc/*.c")

add_custom_target (
        rrc_flag ALL
        ${PROJECT_SOURCE_DIR}/tools/asn1c_compile.sh "${PROJECT_SOURCE_DIR}" "${DST_DIR}" "${NGAP_ASN_FILE}" "NR_" "-findirect-choice"
        DEPENDS  "${NGAP_ASN_FILE}"
)

add_library(rrc_asn1_${SYSTEM_CPU_COMPILER} ${RRC_LIB_SRCS})
add_dependencies(rrc_asn1_${SYSTEM_CPU_COMPILER} rrc_flag)

file(GLOB_RECURSE ASN1C_LIB_SRCS "${PROJECT_SOURCE_DIR}/skeletons/*.c")
add_library(asn1c_${SYSTEM_CPU_COMPILER} ${ASN1C_LIB_SRCS})

#add_executable(asn1clib main.c)